<?php
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class UsersController extends AppController {

    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('User invalide'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('L\'user a été sauvegardé'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('L\'user n\'a pas été sauvegardé. Merci de réessayer.'));
            }
        }
    }

    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('User Invalide'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('L\'user a été sauvegardé'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('L\'user n\'a pas été sauvegardé. Merci de réessayer.'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
    }

    public function delete($id = null) {
        // Avant 2.5, utilisez
        // $this->request->onlyAllow('post');

        $this->request->allowMethod('post');

        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('User invalide'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User supprimé'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('L\'user n\'a pas été supprimé'));
        return $this->redirect(array('action' => 'index'));
    }

    public function login() {
        $this->layout = null;
        if ($this->request->is('post')) {
            $passwordHasher = new SimplePasswordHasher();
            $user = $this->User->find('first', array('conditions' => array('User.username' => $this->request->data['username'], 'User.password' => $passwordHasher->hash(
                $this->request->data['password']))));
            if($user != null){
                $token = uniqid();
                $user['User']['token'] = $token;
                $user['User']['date_token'] = null;
                $this->User->id = $user['User']['id'];
                $this->User->save($user);
                $result = new \stdClass();
                $result->code = 0;
                $result->body = $token;
                $this->set(array(
                    'result' => $result,
                    '_serialize' => array('result')
                ));
            }else{
                $result = new \stdClass();
                $result->code = 1;
                $result->body = "Compte invalide";
                $this->set(array(
                    'result' => $result,
                    '_serialize' => array('result')
                ));
            }

        }
	}

	public function logout() {
	    return $this->redirect($this->Auth->logout());
	}

}
